﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetLocationAndPoiInfo : MonoBehaviour {

	public Text locText;
	public Text poiText;
	public Button locBtn;
	public Button poiBtn;

	AndroidJavaClass jc;
	AndroidJavaObject jo;

	// Use this for initialization
	void Start () {
		try {
			OnStart ();
			locBtn.onClick.AddListener (() => {
				GetLocationInfo ();
			});
			poiBtn.onClick.AddListener (() => {
				GetPoiInfo ();
			});
		} catch (System.Exception ex) {
			Debug.LogError ("getlocation start error:" + ex.Message);
		}
	}
	void OnStart() {
		jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
	}

	void GetLocationInfo() {
		try {
			locText.text = "";
			OnStart ();
			locText.text = jo.Call<string> ("GetInfo");
		} catch (System.Exception ex) {
			Debug.LogError ("GetLocationInfo error:" + ex.Message);
		}
	}

	void GetPoiInfo() {
		try {
			poiText.text = "";
			LocationInfo lti = Input.location.lastData;
			
			//Input.gyro.attitude 返回值为 Quaternion类型，即设备旋转欧拉角  
			Quaternion attitude = Input.gyro.attitude;
			//获取设备重力加速度向量  
			Vector3 deviceGravity = Input.gyro.gravity;  
			//设备的旋转速度，返回结果为x，y，z轴的旋转速度，单位为（弧度/秒）  
			Vector3 rotationVelocity = Input.gyro.rotationRate;  
			//获取更加精确的旋转  
			Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;  
			//设置陀螺仪的更新检索时间，即隔 0.1秒更新一次  
			Input.gyro.updateInterval = 0.1f;  
			//获取移除重力加速度后设备的加速度  
			Vector3 acceleration = Input.gyro.userAcceleration; 
			
			string res = 
				"经度:" + lti.longitude +
				";纬度:" + lti.latitude +
				";高度:" + lti.altitude +
				";罗盘度数:" + Input.compass.trueHeading +
				";陀螺仪欧拉旋转角:" + Input.gyro.attitude +
				";陀螺仪设备重力加速度向量:" + Input.gyro.gravity +
				";陀螺仪旋转速度:" + Input.gyro.rotationRate +
				";陀螺仪更加精确的旋转:" + Input.gyro.rotationRateUnbiased +
				";陀螺仪更新检索时间:" + Input.gyro.updateInterval +
				";陀螺仪移除重力加速度后设备的加速度:" + Input.gyro.userAcceleration;
			poiText.text = res;
			//		OnStart();
			//		poiText.text = jo.Call<string>("GetPoi", "酒店", "", 1);
		} catch (System.Exception ex) {
			Debug.LogError ("GetPoiInfo error:" + ex.Message);
		}
	}
}